﻿using System;
using System.Collections;
using System.Net.Mail;
using System.Text;
using log4net;

namespace SDL
{
    public static class MailSender
    {
        public static string MailEncoding = "koi8";

        public static string SmtpServer { get; set; }
        public static string SmtpAccountUsername { get; set; }
        public static string SmtpAccountPassowrd { get; set; }
        public static int SmtpPort { get; set; }

        private static ILog log = Log.GetLogger(typeof(MailSender));

        static MailSender()
        {
            SmtpPort = 25;
        }

        public static bool SendMessage(string FromMail, string strTo, string strSubject, string strPath, Hashtable vars)
        {
            var tpl = new Parser(strPath, vars);
            return (SendMessage(FromMail, strTo, strSubject, tpl.Parse()));
        }

        public static bool SendMessage(string strFrom, string strTo, string strSubject, string strBody)
        {
            var from = new MailAddress(strFrom);

            var to = new MailAddress(strTo);
            var message = new MailMessage(from, to);

            //message.Headers.Add("content-type", "text/html; charset=" + MailEncoding);
            message.Body = strBody;
            message.BodyEncoding = Encoding.GetEncoding(MailEncoding);
            message.IsBodyHtml = true;

            message.Subject = strSubject;
            message.SubjectEncoding = Encoding.GetEncoding(MailEncoding);

            bool res = true;

            try
            {
                var smtpClient = new SmtpClient(SmtpServer)
                {
                    Credentials =
                    new System.Net.NetworkCredential(SmtpAccountUsername,
                                                     SmtpAccountPassowrd)
                };
                smtpClient.Port = SmtpPort;
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                log.Error("SEND MAIL ERROR: ", ex);
                log.Error(ex.StackTrace);
                while (ex.InnerException != null)
                {
                    log.Error("inner", ex.InnerException);
                    ex = ex.InnerException;
                }
                throw ex;
                res = false;
            }

            return res;
        }

        //public static bool SendMessage(string strFrom, string strTo, string strSubject, string strBody)
        //{
        //    bool res = true;
        //    try
        //    {
        //        EmailMessage emailMessage = new EmailMessage();
        //        emailMessage.FromAddress = new EmailAddress(strFrom);
        //        emailMessage.AddToAddress(new EmailAddress(strTo));
        //        emailMessage.Subject = strSubject;
        //        emailMessage.BodyText = strBody;
        //        SmtpServer smtpServer = new SmtpServer(SmtpServer);
        //        smtpServer.SmtpAuthToken = new SmtpAuthToken(SmtpAccountUsername, SmtpAccountPassowrd);
        //        //smtpServer.SetField("Port", SmtpPort);
        //        emailMessage.Send(smtpServer);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error("SEND MAIL ERROR: ", ex);
        //        log.Error(ex.StackTrace);
        //        while (ex.InnerException != null)
        //        {
        //            log.Error("inner", ex.InnerException);
        //            ex = ex.InnerException;
        //        }
        //        throw ex;
        //        res = false;
        //    }
        //    return res;
        //}

    }
}
